package com.leyou.test;

import com.leyou.common.dto.SpecParamDTO;
import com.leyou.common.dto.SpuDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.item.client.ItemClient;
import com.leyou.search.pojo.Goods;
import com.leyou.search.repository.GoodsRepository;
import com.leyou.search.service.GoodsService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FeignTestDemo {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsRepository goodsRepository;

    @Test
    public void saveGoods(){
        int page = 1,size = 100, totalPage = 0;
        do{
            PageResult<SpuDTO> spuDTOPageResult = itemClient.spuPageQuery(page, size, null, true);
            totalPage = spuDTOPageResult.getTotalPage();
            //获取spu集合
            List<SpuDTO> spuDTOS = spuDTOPageResult.getItems();
            //把所有的SPU变成goods对象集合
            List<Goods> goodsList = spuDTOS.stream().map(goodsService::buildGoods).collect(Collectors.toList());
            //写入索引库
            goodsRepository.saveAll(goodsList);
            page++;
        }while(page<=totalPage);


    }

    @Test
    public void findParams(){
        List<SpecParamDTO> params = itemClient.findParams(null, 76l, null);
        System.out.println(params);
    }
}
